package com.blog.service.Impl;

import com.blog.dto.UserDTO;
import com.blog.dto.UserEditPwd;
import com.blog.dto.UserLoginDTO;
import com.blog.entity.User;
import com.blog.mapper.UserMapper;
import com.blog.service.UserService;
import com.blog.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserVO> getAll() {
        List<User> userList = userMapper.selectALl();
        List<UserVO> UserVOList = new ArrayList<>();
        userList.forEach((user) -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            UserVOList.add(userVO);
        });
        return UserVOList;
    }

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        //密码比对
        // 对前端传输过来的password进行md5加密，然后再进行比对
        String password = DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        userLoginDTO.setPassword(password);
        User user = userMapper.getUserByIAndPassword(userLoginDTO);
        return user;
    }

    @Override
    public List<UserVO> listByName(String name) {
        List<User> userList = userMapper.getUserByName(name);
        List<UserVO> UserVOList = new ArrayList<>();
        userList.forEach((user) -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            UserVOList.add(userVO);
        });
        return UserVOList;
    }

    @Override
    public UserVO listById(Long id) {
        UserVO userVO = new UserVO();
        User user = userMapper.getUserById(id);
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @Override
    public void AddUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userMapper.insert(user);
    }

    @Override
    public void EditUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userMapper.Update(user);
    }

    @Override
    public void DeleteUser(Long id) {
        userMapper.delete(id);
    }

    @Override
    public boolean EditUserPwd(UserEditPwd userEditPwd) {
        //先查询是否有这个人（根据账号密码）
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        userLoginDTO.setUserId(userEditPwd.getId());
        userLoginDTO.setPassword(userEditPwd.getOldPwd());
        //加密密码
        String password = DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes());
        userLoginDTO.setPassword(password);
        User user = userMapper.getUserByIAndPassword(userLoginDTO);
        //有的话：修改密码
        if (user != null){
            String newPassword = DigestUtils.md5DigestAsHex(userEditPwd.getNewPwd().getBytes());
            userEditPwd.setNewPwd(newPassword);
            userMapper.updatePwd(userEditPwd);
            return true;
        }
        return false;
        //没有的话：返回提示
    }
}
